local mingjing = fk.CreateSkill{
  name = "lingling__mingjing",
}

Fk:loadTranslationTable{
  ["lingling__mingjing"] = "明镜",
  [":lingling__mingjing"] = "准备或结束阶段，你可以令一名角色将手牌数向另一名角色尽可能调整（至多摸或弃置三张牌，其中一者须为你）。"..
  "若其因此弃置了至少两张牌，其可以视为对你使用【杀】。",

  ["#lingling__mingjing-choose"] = "明镜：选择包括你在内的两名角色，前者将手牌调整至后者，若其弃两张牌则可以视为对你使用【杀】",
  ["#lingling__mingjing-slash"] = "明镜：是否视为对 %src 使用【杀】？",
}

mingjing:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(mingjing.name) and
      (player.phase == Player.Start or player.phase == Player.Finish) and
      table.find(player.room.alive_players, function (p)
        return table.find(player.room.alive_players, function (q)
          return p:getHandcardNum() ~= math.min(q:getHandcardNum(), 5)
        end) ~= nil
      end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local success, dat = room:askToUseActiveSkill(player, {
      skill_name = "#lingling__mingjing_active",
      prompt = "#lingling__mingjing-choose",
    })
    if success and dat then
      event:setCostData(self, {tos = dat.targets})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local p2 = event:getCostData(self).tos[2]
    local n = to:getHandcardNum() - p2:getHandcardNum()
    if n > 0 then
      n = math.min(n, 3)
      local cards = room:askToDiscard(to, {
        min_num = n,
        max_num = n,
        include_equip = false,
        skill_name = mingjing.name,
        cancelable = false,
      })
      if player.dead or to.dead or #cards < 2 then return end
      if to:canUseTo(Fk:cloneCard("slash"), player, { bypass_distances = true, bypass_times = true }) and
        room:askToSkillInvoke(to, {
          skill_name = mingjing.name,
          prompt = "#lingling__mingjing-slash:"..player.id,
        }) then
        room:useVirtualCard("slash", nil, to, player, mingjing.name, true)
      end
    elseif n < 0 then
      n = math.min(-n, 3)
      to:drawCards(n, mingjing.name)
    end
  end,
})

return mingjing
